package cn.stylefeng.guns.modular.storage.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.storage.entity.StorageStock;
import cn.stylefeng.guns.modular.storage.entity.StorageStockDetailed;
import cn.stylefeng.guns.modular.storage.mapper.StorageStockDetailedMapper;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockDetailedParam;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockDetailedResult;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import  cn.stylefeng.guns.modular.storage.service.StorageStockDetailedService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 库存流水 服务实现类
 */
@Service
public class StorageStockDetailedServiceImpl extends ServiceImpl<StorageStockDetailedMapper, StorageStockDetailed> implements StorageStockDetailedService {

    @Override
    public void add(StorageStockDetailedParam param){
        StorageStockDetailed entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StorageStockDetailedParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StorageStockDetailedParam param){
        StorageStockDetailed oldEntity = getOldEntity(param);
        StorageStockDetailed newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StorageStockDetailedResult findBySpec(StorageStockDetailedParam param){
        return null;
    }

    @Override
    public List<StorageStockDetailedResult> findListBySpec(StorageStockDetailedParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StorageStockDetailedParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageByGoodsSpec(StorageStockDetailedParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findPageByGoodsSpec(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StorageStockDetailedParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StorageStockDetailed getOldEntity(StorageStockDetailedParam param) {
        return this.getById(getKey(param));
    }

    private StorageStockDetailed getEntity(StorageStockDetailedParam param) {
        StorageStockDetailed entity = new StorageStockDetailed();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    
    @Resource
    BasicAreaService basicAreaService;
    
    @Resource
    BasicGoodsService basicGoodsService;
    
    @Resource
    UserService userService;
    
    /**
     * 批量新增 库存流水
     * @param operation 业务名称
     * @param orderNumber 单号
     * @param type 类型
     * @param content 内容
     * @param ssList 库存list
     */
	@Override
	public void batchAdd(String operation, String orderNumber, String type, String content, List<StorageStock> ssList,User user) {
		StorageStockDetailedParam param = new StorageStockDetailedParam();
		for (StorageStock s : ssList) {
			BasicGoods basicGoods = basicGoodsService.getById(s.getGoodsId());
			param.setStockId(Long.parseLong(s.getId().toString()));
			param.setOperation(operation);
			param.setOrderNumber(orderNumber);
			param.setType(type);
			param.setContent(content);
			param.setWarehouseId(s.getWarehouseId());
			param.setWarehouseName(s.getWarehouseName());
			param.setAreaId(s.getAreaId());
			param.setAreaName(basicAreaService.getById(s.getAreaId()).getAreaName());
			param.setPositionId(s.getPositionId());
			param.setPositionNumber(s.getPositionNumber());
			param.setTrayId(s.getTrayId());
			param.setTrayNumber(s.getTrayNumber());
			param.setGoodsId(s.getGoodsId());
			param.setGoodsName(s.getGoodsName());
			param.setGoodsNumber(s.getGoodsNumber());
			param.setGoodsUnit(basicGoods.getGoodsUnit());
			param.setGoodsSpecs(basicGoods.getGoodsSpecs());
			param.setPutTime(s.getPutTime());
			param.setGoodsbatchs(s.getGoodsbatchs());
			param.setNum(s.getNum());
			param.setWeight(s.getStockWeight());
			param.setVolume(s.getStockVolume());
			param.setMoney(s.getStockMoney());
			param.setDeptId(user.getDeptId());
			param.setDeliverId(user.getDeliverId());
			param.setCreateName(user.getName());
			param.setCreateAccount(user.getAccount());
			this.add(param);
		}
	}
	
	/**
     * 批量新增 库存流水
     * @param operation 业务名称
     * @param orderNumber 单号
     * @param type 类型
     * @param content 内容
     * @param ssp 库存
     */
	@Override
	public void addNewDetailed(String operation, String orderNumber, String type, String content, StorageStockParam s,User user) {
		StorageStockDetailedParam param = new StorageStockDetailedParam();
		BasicGoods basicGoods = basicGoodsService.getById(s.getGoodsId());
		param.setStockId(Long.parseLong(s.getId().toString()));
		param.setOperation(operation);
		param.setOrderNumber(orderNumber);
		param.setType(type);
		param.setContent(content);
		param.setWarehouseId(s.getWarehouseId());
		param.setWarehouseName(s.getWarehouseName());
		param.setAreaId(s.getAreaId());
		param.setAreaName(basicAreaService.getById(s.getAreaId()).getAreaName());
		param.setPositionId(s.getPositionId());
		param.setPositionNumber(s.getPositionNumber());
		param.setTrayId(s.getTrayId());
		param.setTrayNumber(s.getTrayNumber());
		param.setGoodsId(s.getGoodsId());
		param.setGoodsName(s.getGoodsName());
		param.setGoodsNumber(s.getGoodsNumber());
		param.setGoodsUnit(basicGoods.getGoodsUnit());
		param.setGoodsSpecs(basicGoods.getGoodsSpecs());
		param.setPutTime(s.getPutTime());
		param.setGoodsbatchs(s.getGoodsbatchs());
		param.setNum(s.getNum());
		param.setWeight(s.getStockWeight());
		param.setVolume(s.getStockVolume());
		param.setMoney(s.getStockMoney());
		param.setDeptId(user.getDeptId());
		param.setDeliverId(user.getDeliverId());
		param.setCreateName(user.getName());
		param.setCreateAccount(user.getAccount());
		this.add(param);
	}
	
	/**
     * 批量新增 库存流水
     * @param operation 业务名称
     * @param orderNumber 单号
     * @param type 类型
     * @param content 内容
     * @param ssp 库存
     */
	@Override
	public void addNewDetailedResult(String operation, String orderNumber, String type, String content, StorageStockResult s,User user) {
		StorageStockDetailedParam param = new StorageStockDetailedParam();
		BasicGoods basicGoods = basicGoodsService.getById(s.getGoodsId());
		param.setStockId(Long.parseLong(s.getId().toString()));
		param.setOperation(operation);
		param.setOrderNumber(orderNumber);
		param.setType(type);
		param.setContent(content);
		param.setWarehouseId(s.getWarehouseId());
		param.setWarehouseName(s.getWarehouseName());
		param.setAreaId(s.getAreaId());
		param.setAreaName(basicAreaService.getById(s.getAreaId()).getAreaName());
		param.setPositionId(s.getPositionId());
		param.setPositionNumber(s.getPositionNumber());
		param.setTrayId(s.getTrayId());
		param.setTrayNumber(s.getTrayNumber());
		param.setGoodsId(s.getGoodsId());
		param.setGoodsName(s.getGoodsName());
		param.setGoodsNumber(s.getGoodsNumber());
		param.setGoodsUnit(basicGoods.getGoodsUnit());
		param.setGoodsSpecs(basicGoods.getGoodsSpecs());
		param.setPutTime(s.getPutTime());
		param.setGoodsbatchs(s.getGoodsbatchs());
		param.setNum(s.getNum());
		param.setWeight(s.getStockWeight());
		param.setVolume(s.getStockVolume());
		param.setMoney(s.getStockMoney());
		param.setDeptId(user.getDeptId());
		param.setDeliverId(user.getDeliverId());
		param.setCreateName(user.getName());
		param.setCreateAccount(user.getAccount());
		this.add(param);
	}
	
	/**
     * 批量新增 库存流水
     * @param operation 业务名称
     * @param orderNumber 单号
     * @param type 类型
     * @param content 内容
     * @param ssp 库存
     */
	@Override
	public void addNewDetailedstorageStock(String operation, String orderNumber, String type, String content, StorageStock s,User user) {
		StorageStockDetailedParam param = new StorageStockDetailedParam();
		BasicGoods basicGoods = basicGoodsService.getById(s.getGoodsId());
		param.setStockId(Long.parseLong(s.getId().toString()));
		param.setOperation(operation);
		param.setOrderNumber(orderNumber);
		param.setType(type);
		param.setContent(content);
		param.setWarehouseId(s.getWarehouseId());
		param.setWarehouseName(s.getWarehouseName());
		param.setAreaId(s.getAreaId());
		if(ToolUtil.isNotEmpty(s.getAreaId())){
			param.setAreaName(basicAreaService.getById(s.getAreaId()).getAreaName());
		}
		param.setPositionId(s.getPositionId());
		param.setPositionNumber(s.getPositionNumber());
		param.setTrayId(s.getTrayId());
		param.setTrayNumber(s.getTrayNumber());
		param.setGoodsId(s.getGoodsId());
		param.setGoodsName(s.getGoodsName());
		param.setGoodsNumber(s.getGoodsNumber());
		if(basicGoods != null) {
			param.setGoodsUnit(basicGoods.getGoodsUnit());
			param.setGoodsSpecs(basicGoods.getGoodsSpecs());
		}
		param.setPutTime(s.getPutTime());
		param.setGoodsbatchs(s.getGoodsbatchs());
		param.setNum(s.getNum());
		param.setWeight(s.getStockWeight());
		param.setVolume(s.getStockVolume());
		param.setMoney(s.getStockMoney());
		param.setDeptId(user.getDeptId());
		param.setDeliverId(user.getDeliverId());
		param.setCreateName(user.getName());
		param.setCreateAccount(user.getAccount());
		this.add(param);
	}

}
